package com.feige.async;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author feige<br />
 * @ClassName: TaskExecutorPoolConfig <br/>
 * @Description: <br/>
 * @date: 2022/11/3 20:16<br/>
 */
@Configuration
@EnableAsync
public class TaskExecutorPoolConfig {
    
    
    @Bean("asyncTaskExecutor")
    public Executor asyncExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors() / 2);
        taskExecutor.setMaxPoolSize(Runtime.getRuntime().availableProcessors());
        taskExecutor.setQueueCapacity(100);
        taskExecutor.setKeepAliveSeconds(30);
        taskExecutor.setThreadNamePrefix("asyncTaskExecutor-");
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return taskExecutor;
    }
    
    
}
